package org.grayrabbit.cms.service.generate.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.grayrabbit.cms.constant.ConstantTheme;
import org.grayrabbit.cms.dao.category.CategoryDao;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.service.generate.CategoryGeneraterService;
import org.grayrabbit.cms.util.FilePathSptUtil;
import org.grayrabbit.cms.vo.category.CategoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;

import org.grayrabbit.service.impl.GenericServiceImpl;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
@Service
@Transactional
public class CategoryGeneraterServiceImpl extends GenericServiceImpl implements CategoryGeneraterService {
	private  static final Logger  log = LoggerFactory.getLogger(CategoryGeneraterServiceImpl.class);
	@Resource
	private Configuration config;
	
	@Autowired
	@Qualifier("categoryDaoImpl")
	private CategoryDao categoryDao;
	
	public boolean publicCategoryTax(String taxType) {
		try {
			String realThemeRootpath = FilePathSptUtil.THEME_ROOT_PATH
					+ ConstantTheme.THEME;
			try {
				ApplicationContext context = WebApplicationContextUtils
						.getRequiredWebApplicationContext(ContextLoader
								.getCurrentWebApplicationContext()
								.getServletContext());
				config = (Configuration) context
						.getBean("freemarkerConfiguration");
				config.setDirectoryForTemplateLoading(new File(
						realThemeRootpath));
			} catch (IOException e) {
				log.info("远程调用接口失败：{}", e);
				return false;
			}
			config.setObjectWrapper(new DefaultObjectWrapper());
			CategoryBean categoryBean = new CategoryBean();
			List<Category> cl = categoryDao.findByPage(categoryBean);
			Category cat = categoryDao.findById(cl.get(0).getCategoryId());
			categoryDao.initialize(cat.getCategory());
			categoryDao.initialize(cat.getCategory().getCategory());
			List<String> list = new ArrayList<String>();
			list.add(cl.get(0).getCategory().getCategoryId());
			list.add(cl.get(0).getCategory().getCategory().getCategoryId());
			
		} catch (Exception e) {
			log.info("远程调用接口失败：{}", e);
			return false;
		}
		return true;
	}

}
